package com.freestyle.seatabusiness.storage.service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.freestyle.seatabusiness.api.service.StockService;
import com.freestyle.seatabusiness.storage.dao.StockTblMapper;
import com.freestyle.seatabusiness.storage.entity.StockTbl;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * Created by rocklee on 2021/12/16 15:53
 */
@DubboService(cluster = "failfast") //不做任何重试
public class StockServiceImp implements StockService {
  @Resource
  private StockTblMapper stockTblMapper;
  @Override
  public void deduct(String commodityCode, int count) {
    StockTbl stock=stockTblMapper.selectOne(Wrappers.lambdaQuery(StockTbl.class)
            .eq(StockTbl::getCommodityCode,commodityCode));
    if (stock==null){
      throw new RuntimeException("无效的商品编码:"+commodityCode);
    }
    stock.setCount(stock.getCount()-count);
    if (stock.getCount()<0){
      throw new RuntimeException("库存不足");
    }
    stockTblMapper.update(stock, new UpdateWrapper<StockTbl>().lambda().eq(StockTbl::getCommodityCode,commodityCode));
  }
}
